#!/usr/local/bin/python

import shutil
import os

try:
    import argparse
    
    parser = argparse.ArgumentParser(description=\
        'Copies a template case from the cases folder to the run folder')

    # Required input
    parser.add_argument('-o', dest='runName', 
                        help='Name of the run folder to create')
            
    parser.add_argument('-f', dest='force', action='store_true', 
           help='Force case creation - WARNING - ' \
            +'This will overwrite existing case and results!!!')
            
    parser.add_argument('caseName', nargs='?', action='store',
                        default=None, help='Name of case folder to copy')

    # Read the inputs
    args = parser.parse_args()
    force = args.force
    caseName = args.caseName
    runName = args.runName

    
except ImportError:
    import optparse

    parser = optparse.OptionParser(description=\
        'Copies a template case from the cases folder to the run folder')

    # Required input
    parser.add_option('-o', dest='runName',
                      help='Name of the run folder to create')

    # Optional inputs
    parser.add_option('-f', dest='force', action='store_true', 
           help='Force case creation - WARNING - ' \
            +'This will overwrite existing case and results!!!',default=False)
            
    # Read the inputs
    (options,args) = parser.parse_args()
    force = options.force
    runName = options.runName
    caseName = args[0]
    
    
# Run folder name defaults to the same as the case folder
if runName is None:
    runName = caseName

# Check if destination folder exists
runPath = os.path.join(os.getcwd(),'../run')
newPath = os.path.join(runPath,runName)
basePath = os.path.join(os.getcwd(),caseName)

if not os.path.isdir(basePath):
    raise IOError('Specified case not found')

if not os.path.isdir(runPath):
    os.mkdir(runPath)
    
if os.path.isdir(newPath):
    if not force:
        raise IOError('Destination case folder already exists')
    else:
        shutil.rmtree(newPath)
        
# Copy case to run folder
shutil.copytree(basePath, newPath)


